<template>
  <!-- 检验项目指标库 -->
  <p-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    :destroyOnClose="true"
    cancelText="关闭"
  >
    <p-spin :spinning="confirmLoading">
      <p-form :form="form">
        <p-row>
          <p-col :span="24">
            <p-form-item label="检验项目编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input readOnly
                class="ant-input ant-input"
                v-decorator="[ 'inspectProjectCode', validatorRules.inspectProjectCode]"
                placeholder="请输入检验项目编码"
              ></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="24">
            <p-form-item label="检验项目名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input
                v-decorator="[ 'inspectProjectName', validatorRules.inspectProjectName]"
                placeholder="请输入检验项目名称"
              ></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="24">
            <p-form-item label="检验标准" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input
                v-decorator="[ 'inspectStandard', validatorRules.inspectStandard]"
                placeholder="请输入检验标准"
              ></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="24">
            <p-form-item label="检验值类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                type="list"
                v-decorator="['inspectValueType', validatorRules.inspectValueType]"
                :trigger-change="true"
                dictCode="inspect_value_type"
                placeholder="请选择检验值类型"
              />
            </p-form-item>
          </p-col>
          <p-col :span="24">
            <p-form-item label="检验手段" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input
                v-decorator="[ 'inspectMethod', validatorRules.inspectMethod]"
                placeholder="请输入检验手段"
              ></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="24">
            <p-form-item label="项目分类" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                type="list"
                v-decorator="['projectClassify', validatorRules.projectClassify]"
                :trigger-change="true"
                dictCode="mes_project_classify_type"
                placeholder="请选择项目分类"
              />
            </p-form-item>
          </p-col>
          <p-col :span="24">
            <p-form-item label="项目类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                type="list"
                v-decorator="['projectType', validatorRules.projectType]"
                :trigger-change="true"
                dictCode="project_type"
                placeholder="请选择项目类型"
              />
            </p-form-item>
          </p-col>
          <p-col :span="24">
            <p-form-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                type="radio"
                v-decorator="['status', validatorRules.status]"
                :trigger-change="true"
                dictCode="mes_check"
                placeholder="请选择状态"
              />
            </p-form-item>
          </p-col>

          <p-col :span="24">
            <p-form-item label="父级节点" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-tree-select
                ref="treeSelect"
                placeholder="请选择父级节点"
                v-decorator="['pid', validatorRules.pid]"
                dict="mes_qc_inspect_index_library,project_type,id"
                pidField="pid"
                pidValue="0"
                hasChildField="has_child"
              ></j-tree-select>
            </p-form-item>
          </p-col>
        </p-row>
      </p-form>
    </p-spin>
  </p-modal>
</template>

<script>
import { httpAction, putAction, api } from "@/api/manage";
import { SubmitOnchangMixin } from "@/mixins/SubmitOnchangMixin";
import pick from "lodash.pick";
import { validateDuplicateValue } from "@/utils/util";
import JDictSelectTag from "@/components/dict/JDictSelectTag";
import JTreeSelect from "@/components/jeecg/JTreeSelect";

export default {
  name: "MesQcInspectIndexLibraryModal",
  mixins: [SubmitOnchangMixin],
  components: {
    JDictSelectTag,
    JTreeSelect,
  },
  data() {
    return {
      form: this.$form.createForm(this),
      title: "操作",
      width: 800,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      visible: false,
      model: {},
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        inspectProjectCode: {
          rules: [{ required: false, message: "请输入检验项目编码!" }],
        },
        inspectProjectName: {
          rules: [{ required: false, message: "请输入检验项目名称!" }],
        },
        inspectStandard: {
          rules: [{ required: false, message: "请输入检验标准!" }],
        },
        inspectValueType: {
          rules: [{ required: true, message: "请输入检验值类型!" }],
        },
        inspectMethod: {
          rules: [{ required: true, message: "请输入检验手段!" }],
        },
        projectClassify: {
          rules: [{ required: true, message: "请输入项目分类!" }],
        },
        projectType: {
          rules: [{ required: true, message: "请输入项目类型!" }],
        },
        inspectStandard: {
          rules: [{ required: true, message: "请输入检验标准!" }],
        },
        status: { rules: [] },
      },
      url: {
        add: "/mes-quality/mesQcInspectIndexLibrary/add",
        edit: "/mes-quality/mesQcInspectIndexLibrary/edit",
      },
      expandedRowKeys: [],
      pidField: "pid",
    };
  },
  created() {},
  methods: {
    add() {
      this.edit({});
      this.getRuleCodeValue();
      // this.model.status = 1;
    },
    getRuleCodeValue() {
      let param = {
        commonFormData: this.model,
        rules: [
          {
            ruleCode: "mes_inspect_project_rule",
            columnName: "inspectProjectCode",
          },
        ],
      };

      putAction(api.ruleBaseURL, param).then((res) => {
        // 执行成功，获取返回的值，并赋到页面上
        if (res.success) {
          console.log("生成的编码：" , res.data);
          const item = res.data;
          item['status'] = 1;//表单默认值赋值区域
          this.form.setFieldsValue(pick(item, Object.keys(item)));
        }
      });
    },
    edit(record) {
      this.form.resetFields();
      this.model = Object.assign({}, record);
      this.visible = true;
      this.$nextTick(() => {
        this.form.setFieldsValue(
          pick(
            this.model,
            "inspectProjectCode",
            "inspectProjectName",
            "inspectValueType",
            "inspectMethod",
            "projectClassify",
            "projectType",
            "inspectStandard",
            "createBy",
            "createTime",
            "updateBy",
            "updateTime",
            "status",
            "remark",
            "pid"
          )
        );
      });
    },
    close() {
      this.$emit("close");
      this.visible = false;
    },
    handleOk() {
      const that = this;
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true;
          let httpurl = "";
          let method = "";
          let formData = Object.assign(this.model, values);
          this.formData = Object.assign(this.model, values);
          //新增触发
          if (!this.model.id) {
            httpurl += this.url.add;
            method = "post";
            if (this.EnhanceJS.afterAdd) {
              //新增之前
              let Obj = eval(this.EnhanceJS.afterAdd);
              new Obj(this);
            } else {
              console.log("表单提交数据", formData);
              that.modalFrom(httpurl, formData, method);
            }
          } else {
            //编辑触发
            httpurl += this.url.edit;
            method = "put";
            if (this.EnhanceJS.afterEdit) {
              //编辑之前
              let Obj = eval(this.EnhanceJS.afterEdit);
              new Obj(this);
            } else {
              console.log("表单提交数据", formData);
              that.modalFrom(httpurl, formData, method);
            }
          }
        }
      });
    },
    handleCancel() {
      this.close();
    },
    popupCallback(row) {
      this.form.setFieldsValue(
        pick(
          row,
          "inspectProjectCode",
          "inspectProjectName",
          "inspectValueType",
          "inspectMethod",
          "projectClassify",
          "projectType",
          "inspectStandard",
          "createBy",
          "createTime",
          "updateBy",
          "updateTime",
          "status",
          "remark",
          "pid"
        )
      );
    },
    submitSuccess(formData, flag) {
      if (!formData.id) {
        let treeData = this.$refs.treeSelect.getCurrTreeData();
        this.expandedRowKeys = [];
        this.getExpandKeysByPid(formData[this.pidField], treeData, treeData);
        this.$emit("ok", formData, this.expandedRowKeys.reverse());
      } else {
        this.$emit("ok", formData, flag);
      }
    },
    getExpandKeysByPid(pid, arr, all) {
      if (pid && arr && arr.length > 0) {
        for (let i = 0; i < arr.length; i++) {
          if (arr[i].key == pid) {
            this.expandedRowKeys.push(arr[i].key);
            this.getExpandKeysByPid(arr[i]["parentId"], all, all);
          } else {
            this.getExpandKeysByPid(pid, arr[i].children, all);
          }
        }
      }
    },
  },
};
</script>